package com.hd.shiro2204.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
      // 用于获取IOC容器对象的工具类

@Component
public class ApplicationContextUtils implements ApplicationContextAware {
    private static  ApplicationContext ctx;

    /**
     * SPring ioc容器在创建这个类的对象时，会回调如下方法，并且把IOC容器对象自身作为参数传递进来
     * @param applicationContext
     * @throws BeansException
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            ctx=applicationContext;
    }
    //根据beanName获取bean对象
    public static Object getBean(String beanName){
        return ctx.getBean(beanName);
    }
    //根据Class获取bean对象
    public static <T> T getBean(Class<T> clazz){
        return ctx.getBean(clazz);
    }
}
